/* Copyright 1986-2009 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 3042 $ */
/*                                                                            */
/* $Id: compile.p 3042 2007-12-03 23:13:32Z ejt $ */
/*                                                                            */

define variable cDestination as character initial "build/work/pro/com/qad/shell/report/reports/".
define variable cCompileList as character initial "compile.lst".
define variable cFilename    as character no-undo.
define variable cLogfile     as character initial "compile.log".
define variable lError       as logical   no-undo.

if session:parameter <> "" then
   cCompileList = session:parameter.
   
lError = false.

output to value(cLogfile) append.

if not connected("qaddb") then do:
   put unformatted "ERROR: Database qaddb is not connected" skip.
   lError = true.
end.
if not connected("qadadm") then do:
   put unformatted "ERROR: Database qadadm is not connected" skip.
   lError = true.
end.
file-info:file-name = cCompileList.
if file-info:file-type = ? then do:
   put unformatted "ERROR: No compile list available. Default is compile.lst, or use -param on the command line to specify" skip.
   lError = true.
end.

if not lError then do:
   put unformatted "Compile starting at " + string(now) skip.
   input from value(cCompileList).
   repeat:
      import cFilename.
      run compileProgram(cFilename) no-error.
      if error-status:error then 
         lError = true. 
   end.
   input close.
   put unformatted "Compile completed at " + string(now) skip.
end.

output close.
quit.

/*-----------------------------------------------------------------------------
  Purpose:    Find the directory separator character (WIN/UNIX) in a filename
  Parameters: pcFilename - The filename with path
  Return:     Character  - The directory separator character
  ---------------------------------------------------------------------------*/
function getDirectorySep returns character
   (input pcFilename as character):

   if index(pcFilename,"~/") > 0 then
      return "~/".
   else if index(pcFilename,"~\") > 0 then
      return "~\".
   else
      return "~/".

end function.

/*-----------------------------------------------------------------------------
  Purpose:    Find the directory in a filename
  Parameters: pcFilename - The filename with path
  Return:     Character  - The directory
  ---------------------------------------------------------------------------*/
function getDirectory returns character
   (input pcFilename as character):

   define variable cDirSep as character no-undo.
   define variable cDir    as character no-undo.

   cDirSep = getDirectorySep(pcFilename).
   
   if r-index(pcFilename,cDirSep) = 0 then
      cDir = "".

   else 
      cDir = substring(pcFilename,1,r-index(pcFilename,cDirSep)).

   return cDir.
    
end function.

/*-----------------------------------------------------------------------------
  Purpose:    Determines if a directory exists
  Parameters: pcDirectory - The directory name
  Return:     Logical     - True if the directory exists
  ---------------------------------------------------------------------------*/
function directoryExists returns logical
   (input pcDirectory as character):

   file-info:file-name = pcDirectory.
   if index(file-info:file-type,"D") > 0 then
      return true.
   else
      return false.
end function.

/*-----------------------------------------------------------------------------
  Purpose:    Creates a directory
  Parameters: pcDirectory - The directory name
  Return:     Logical     - True if the directory was created without error
  ---------------------------------------------------------------------------*/
function createDirectory returns logical
   (input pcDirectory as character):

   define variable i as integer no-undo.
   define variable cParent as character no-undo.
   define variable lReturnValue as logical no-undo.

   cParent = substring(pcDirectory,1,r-index(pcDirectory,
                                             getDirectorySep(pcDirectory),
                                             length(pcDirectory) - 1)).

   if not directoryExists(cParent) then do:
      lReturnValue = createDirectory(cParent).
      if lReturnValue = false then
         return lReturnValue.
   end.

   os-create-dir value(pcDirectory).
   
   return (os-error = 0). 

end function.

/*-----------------------------------------------------------------------------
  Purpose:    Compile a program
  Parameters: pcFilename - The name of the file to compile
  Return:     
  ---------------------------------------------------------------------------*/
procedure compileProgram:
   define input parameter pcFilename as character no-undo.

   define variable cDirectory as character no-undo.
   define variable lReturnValue as logical no-undo.

   cDirectory = cDestination + getDirectory(pcFilename).

   if cDirectory <> "" then do:
      if not directoryExists(cDirectory) then do:
         lReturnValue = createDirectory(cDirectory).
         if lReturnValue = false then do:
            put unformatted "ERROR: Cannot create directory " + cDirectory skip.
            return error.
         end.
      end.
   end.
      
   put unformatted "Compiling " + pcFilename skip.
   compile value(pcFilename) save into value(cDestination)/* no-error*/.
 
   if compiler:error then do:
      put unformatted "ERROR: " + error-status:get-message(1) skip.
      return error.
   end.

   return.
end procedure.
